<?php
/**
 * 订单Action
 * @author SongKejing 2012.12.27
 *
 */
class OrderAction extends BaseAction{
	
	/**
	 * 获取订单分页数据
	 */
	public function getOrders(){		
		$order = new OrderModel();
		$begin_time = isset($_POST['begin_time']) ? $_POST['begin_time'] : date('Y-m-d', time()-30*24*3600);
		$end_time = isset($_POST['end_time']) ? $_POST['end_time'] : date('Y-m-d');
		$name = isset($_POST['name']) ? $_POST['name'] : '';
		$tel_num = isset($_POST['tel_num']) ? $_POST['tel_num'] : '';
		$order_status = isset($_POST['order_status']) ? $_POST['order_status'] : 13;
		$current_page = isset($_REQUEST['current_page']) ? $_REQUEST['current_page'] : 1;
		$page_step = isset($_REQUEST['page_step']) ? $_REQUEST['page_step'] : 12;
		$page_count = isset($_REQUEST['page_count']) ? $_REQUEST['page_count'] : 0;
		$orders = $order->getCustomerOrder($begin_time, $end_time, $name, $tel_num, $order_status, $current_page, $page_step, $page_count);
		log::write('orders:'.print_r($orders, true));
		$this->assign('begin_time', $begin_time);
		$this->assign('end_time', $end_time);
		$this->assign('name', $name);
		$this->assign('tel_num', $tel_num);
		$this->assign('order_status', $order_status);		
		$this->assign('current_page', $orders['current_page']);
		$this->assign('page_step', $orders['page_step']);
		$this->assign('page_count', $orders['page_count']);	
		$this->assign('orders', $orders);
		$this->display();
	}
	
	/**
	 * 退款
	 */
	public function refundOrder(){
		$tradeNo = $_POST['tradeNo'];
		$OrderPrice = $_POST['OrderPrice'];
		$orderDetail = array(
			array(
					'0'=>$tradeNo,
					'1'=>$OrderPrice,
					'2'=>'退款'
				)
		);
		$result = refundAlipayApi($orderDetail);
		if($result){
			$order = new OrderModel();
			$order_id = $_POST['order_id'];
			//更新订单状态
			$order->updateOrderStatus($order_id, 3);
			$service_id = $_POST['service_id'];
			//更新客服处理结果
			$result = $order->updateServiceOrderInfo($service_id, $order_id);
			$result = array('result'=>$result, 'msg'=>'退款成功');
		}else{
			$result = array('result'=>$result, 'msg'=>'退款失败');
		}
		exit(json_encode($result));
	}
	
	/**
	 * 导出
	 */
	public function exportOrders(){
		$order = new OrderModel();
		$begin_time = isset($_POST['begin_time']) ? $_POST['begin_time'] : date('Y-m-d', time()-30*24*3600);
		$end_time = isset($_POST['end_time']) ? $_POST['end_time'] : date('Y-m-d');
		$name = isset($_POST['name']) ? $_POST['name'] : '';
		$tel_num = isset($_POST['tel_num']) ? $_POST['tel_num'] : '';
		$order_status = isset($_POST['order_status']) ? $_POST['order_status'] : 1;
		$export_ids = $_POST['export_ids'];
		$order->exportOrders($begin_time, $end_time, $name, $tel_num, $order_status, $export_ids);
		//返回浏览器前一页
		exit("<script>window.history.go(-1);</script>");
	}
	
	/**
	 * 刷新分配的订单
	 */
	public function freshSignOrder(){
		$order = new OrderModel();
		$result = $order->dealOrdersForServices();
		exit(json_encode(array('result'=>$result)));
	}
	
	/**
	 * 获取订单
	 */
	public function getServiceOrders(){
//		log::write('getServiceOrders:'.print_r($_REQUEST, true));
//		log::write('session_id:'.session_id());
//		$result_array = array('result'=>true, 'msg'=>'hahaha');
//		$result = json_encode($result_array);
//		exit($result);
		$begin_time = isset($_POST['begin_time']) ? $_POST['begin_time'] : date('Y-m-d', time()-30*24*3600);
		$end_time = isset($_POST['end_time']) ? $_POST['end_time'] : date('Y-m-d');
		$name = isset($_POST['name']) ? $_POST['name'] : '';
		$tel_num = isset($_POST['tel_num']) ? $_POST['tel_num'] : '';
		$order_status = isset($_POST['order_status']) ? $_POST['order_status'] : 13;
		$current_page = isset($_POST['current_page']) ? $_POST['current_page'] : 1;
		$page_step = isset($_POST['page_step']) ? $_POST['page_step'] : 12;
		$page_count = isset($_POST['page_count']) ? $_POST['page_count'] : 0;
		$user_id = isset($_POST['uid']) ? $_POST['uid'] : 1;
		$order = new OrderModel();
		$result = $order->getServiceOrders($begin_time, $end_time, $name, $tel_num, $order_status, $user_id, $current_page, $page_step, $page_count);
		log::write('$result:'.print_r($result, true));
		exit(json_encode($result['orders']));
	}
	
	public function test(){
		log::write('session_id:'.SESSION::id());
		$a = array(
			array(
				'OrderNumber'=>"12229992",
				'OrderPrice'=>"12299",
				'ctime'=>"2012-13-12 05:05:06",
				'name'=>"张三",
				'mobile'=>"15688889999",
				'status_name'=>"已付款"
			),
			array(
				'OrderNumber'=>"88889999",
				'OrderPrice'=>"122134",
				'ctime'=>"2012-08-12 05:05:06",
				'name'=>"李四",
				'mobile'=>"55874485",
				'status_name'=>"哈哈"
			),
			array(
				'OrderNumber'=>"354646545",
				'OrderPrice'=>"578858",
				'ctime'=>"2012-13-12 05:05:06",
				'name'=>"王五",
				'mobile'=>"4684674567",
				'status_name'=>"已付款"
			),
		);
		exit(json_encode($a));
	}
	
}
?>